package com.jt.huangxiaolong.demos.handler;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jt.huangxiaolong.demos.pojo.User;
import com.jt.huangxiaolong.demos.service.UserService;
import com.jt.huangxiaolong.demos.util.CommonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserHandler {
    @Resource
    UserService userService;

    @RequestMapping("/users")
    @ResponseBody
    public PageInfo<User> users(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 10);
        System.out.println("分页查询控制器");
        List<User> users = userService.allUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(users, 10);
        return pageInfo;
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult saveData(@RequestBody User user) {
        System.out.println(user);
        int res = userService.saveDateCheck(user);
        System.out.println(res);
        if (res == -1) {
            return CommonResult.failed("案件已存在");
        }
        return CommonResult.success(res);
    }

    @GetMapping("/selectByDName")
    @ResponseBody
    public PageInfo<User> selectByDName(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam String dName,@RequestParam String dType,@RequestParam String dState) {
        PageHelper.startPage(pn, 10);
        List<User> users = userService.selectByDName(dName,dType,dState);
        PageInfo<User> pageInfo = new PageInfo<User>(users, 10);
        return pageInfo;
    }
    @RequestMapping("excel")
    @ResponseBody
    public List<User> excel(){
        String fileName = "D:\\workspase\\ideaworkspace\\huangxiaolong\\src\\main\\resources\\excel数据.xls";
        List<User> users = userService.allpage();
        EasyExcel.write(fileName, User.class).sheet("excel").doWrite(users);
        return users;
    }
    @RequestMapping("/updateData")
    @ResponseBody
    public CommonResult updateByPrimaryKey( @RequestBody User user){
        System.out.println(user);
        int i = userService.update(user);
        if (1==i){
            return CommonResult.success(i);
        }else {
            return CommonResult.failed();
        }
    }
    @RequestMapping("/deleteall")
    @ResponseBody
    public String deleteAll(String userlist){
        String[] splits = userlist.split(",");
        List<Integer> ids = new ArrayList<>();
        for (String s : splits){
            ids.add(Integer.parseInt(s));
        }
        userService.deleteAll(ids);
        return "";
    }
    @RequestMapping("/deleteRight")
    @ResponseBody
    public CommonResult deleteByPrimaryKey(@RequestBody @RequestParam("id") Integer id) {
        int i = userService.deleteByPrimaryKey(id);
        return CommonResult.success(i);
    }
    @RequestMapping("/updateall")
    @ResponseBody
    public CommonResult updateById(@RequestBody User user){
        int i = userService.updateAll(user);
        if (1==i){
            return CommonResult.success(i);
        }else {
            return CommonResult.failed();
        }
    }
}
